package com.tianqu.tms.www.transfer.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.core.webservice.weichat.api.WeChatMqApi;
import com.tianqu.tms.core.webservice.weichat.api.rpc.TransferDataSyn000434Bo;
import com.tianqu.tms.dao.TmsDao;
import com.tianqu.tms.dao.TmsTransferFinanceDetailDao;
import com.tianqu.tms.dao.TmsTransferFinanceDetailDaoImp;
import com.tianqu.tms.dao.TransferProcessHistoryDaoImp;
import com.tianqu.tms.dto.TransferAcceptedInfoDto;
import com.tianqu.tms.dto.TransferProcessHistoryDto;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.www.transfer.actionform.CheckConfirmActionForm;

public class StatusRefundControlInputConfirmController extends TmsController {
	private Logger logger = Logger.getLogger(StatusControlInputResultController.class);
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = getTxController();
		try {
			controller.begin();
			CheckConfirmActionForm form = (CheckConfirmActionForm)uc.getInputForm();
			TmsUserProfile profile = uc.getUserProfile();
			//
			//1、更新 过户业务基本信息表 (tms_transfer_accepted_info)
			TransferAcceptedInfoDto transferAcceptedInfoDto = new TransferAcceptedInfoDto();
			transferAcceptedInfoDto.setChangeUsenature(Integer.parseInt(form.getChangeUsenature()));
			transferAcceptedInfoDto.setUpdateUserId(profile.getUniqueId());
			transferAcceptedInfoDto.setSerial(form.getSerial());
			TransferAcceptedInfo.getInstance().updateAcceptInfo003(transferAcceptedInfoDto);
			
			//更新 过户业务财务收费明细表（tms_transfer_finance_detail）
			//# 过户费结算日期（acceptedTime）为当前系统时间
            TmsTransferFinanceDetailDao financeDetailDao = (TmsTransferFinanceDetailDao)TmsTransferFinanceDetailDaoImp.getInstance(TmsDao.TMS_TRANSFER_FINANCE_DETAIL);
            financeDetailDao.updateFinanceDetailAcceptedTime(form.getSerial());
			
			//2、追加 过户流程操作记录表（tms_transfer_process_history）
			TransferProcessHistoryDto transferProcessHistoryDto = new TransferProcessHistoryDto();
			transferProcessHistoryDto.setInsertUserId(profile.getUniqueId());
			if("20".equals(form.getCheckFileStatus()) && "1".equals(form.getChangeUsenature())) {
				transferProcessHistoryDto.setBusinessType(21);
			}else if("20".equals(form.getCheckFileStatus()) && "2".equals(form.getChangeUsenature())) {
				transferProcessHistoryDto.setBusinessType(20);
			}else if("22".equals(form.getCheckFileStatus()) && "1".equals(form.getChangeUsenature())) {
				transferProcessHistoryDto.setBusinessType(36);
			}else if("22".equals(form.getCheckFileStatus()) && "2".equals(form.getChangeUsenature())) {
				transferProcessHistoryDto.setBusinessType(22);
			}else if("25".equals(form.getCheckFileStatus()) && "1".equals(form.getChangeUsenature())) {
				transferProcessHistoryDto.setBusinessType(26);
			}else if("25".equals(form.getCheckFileStatus()) && "2".equals(form.getChangeUsenature())) {
				transferProcessHistoryDto.setBusinessType(25);
			}
			transferProcessHistoryDto.setSerial(form.getSerial());
			transferProcessHistoryDto.setReason(form.getRemarks());
			TransferProcessHistoryDaoImp transferProcessHistoryDao = (TransferProcessHistoryDaoImp) TransferProcessHistoryDaoImp.getInstance(TmsDao.TMS_TRANSFER_PROCESS_HISTORY);
			transferProcessHistoryDao.insertHisInfoBySerial(transferProcessHistoryDto);
			//API 434
			TransferDataSyn000434Bo bo = new TransferDataSyn000434Bo();
			bo.setSerial(form.getSerial());
			bo.setStatus(0);//业务作废
			bo.setMarketNo(TmsConstants.MARKET_NO);
			bo.setBusinessType(transferProcessHistoryDto.getBusinessType());
			WeChatMqApi.getInstance().call(bo);
			
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException)  throw e;
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		logger.debug(this.getClass().getName() + "#execute was completed.");
	}
}
